<?php
/**
 * 客户联系人
 */
require_once("../global.inc.php");

class SysCustomerClass extends SystemFrameWork{
	/**
	 * 客户联系人对象
	 *
	 * @var obj
	 */
	var $obj_customer;
	/**
	 * 银行账户对象
	 *
	 * @var obj
	 */
	var $obj_bank;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	
	function main(){
		/**
		 * 银行账户
		 */
		if (!is_object($this->obj_customer)){
			require_once ("bank.class.php");
			$this->obj_bank = new BankClass();
		}
		/**
		 * 客户联系人
		 */
		if (!is_object($this->obj_customer)){
			require_once ("customer.class.php");
			$this->obj_customer = new CustomerClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
			case "add":
				$this->_add();
				break;
			case "save":
				$this->_save();
				break;
			case "modi":
				$this->_modi();
				break;
			case "update":
				$this->_update();
				break;
			case "view":
				$this->_view();
				break;
			case "del":
				$this->_del();
				break;
			case "ajax_get_customer":
				$this->_ajax_get_customer();
				break;
			default:
				$this->_list();
		}
	}
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax类表
	 */
	function _ajax_list(){
		$type = intval($this->_input['t']);
		//搜索条件
		$condition = " where customer.type='".$type."' and customer.status='1' order by customer.id desc ";

		//信息列表
		$this->obj_page->pagebarnum(10);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$customer_array = $this->obj_customer->listCustomer($condition,$this->obj_page);
		
		//对时间进行格式化
		if (is_array($customer_array)){
			for ($i=0;$i<count($customer_array);$i++){
				$customer_array[$i]['create_time'] = @date("Y-m-d",$customer_array[$i]['create_time']);
			}
		}

		$page_list = $this->obj_page->show(2);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('customer_array',$customer_array);
		$this->output('page_list',$page_list);
		$this->output('type',$type);
		$this->showpage('customer.list');
	}
	/**
	 * 信息添加
	 */
	function _add(){
		$type = intval($this->_input['t']);
		if ($type != 1 && $type != 2 && $type != 3) {
			$type = 1;
		}
		$this->output('type',$type);
		$this->showpage('customer.add');
	}
	/**
	 * 信息添加
	 */
	function _save(){
		$type = intval($this->_input['t']);
		$typename = '';
		if ($type == 1) {
			$typename = '客户联系人';
		}elseif ($type == 2){
			$typename = '媒介联系人';
		}elseif ($type == 3){
			$typename = '供应商联系人';
		}
		$value_array = array();
		$value_array['type'] = $type;  //类型（1客户 2媒体 3供应商）
		$value_array['company_id'] = $this->_input["hidCompanyId"];  //客户ID
		$value_array['realname'] = trim($this->_input["realname"]);  //名称
		$value_array['gender'] = $this->_input["gender"];  //性别
		$value_array['jobname'] = trim($this->_input["jobname"]);  //职位头衔
		$value_array['mobile'] = trim($this->_input["mobile"]);  //手机
		$value_array['office_call'] = trim($this->_input["office_call"]);  //办公电话
		$value_array['email'] = trim($this->_input["email"]);  //邮箱
		$value_array['im'] = trim($this->_input["im"]);  //MSN，QQ
		$value_array['address'] = trim($this->_input["address"]);  //地址
		$value_array['post'] = trim($this->_input["post"]);  //邮编
		$value_array['create_id'] = $_SESSION['s_admin']['id'];  //创建者ID
		$value_array['create_time'] = time();  //创建时间
		$value_array['status'] = 1;  //状态
		$result = $this->obj_customer->addCustomer($value_array);
		
		if ($result != true){
			$this->redirectPath('error','','添加'.$typename.'失败！',1);
		}else {
			//记录操作日志
			SystemPowerClass::addSysLog($typename.'->'.$typename.'管理->添加'.$typename);
			$this->redirectPath('error','./customer.manage.php?action=list&t='.$type,'添加'.$typename.'成功！',1);
		}
	}
	
	/**
	 * 修改信息
	 */
	function _modi(){
		$id = $this->_input["id"];
		$type = $this->_input["t"];
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$id, "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$customer_array = $this->obj_customer->getCustomer($id, 'more');
			
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('customer_array',$customer_array);
			$this->output('type',$type);
			$this->showpage('customer.modi');
		}
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsIdIsEmpty']),
//			array("input"=>$this->_input["txttitle"], "require"=>"true", "message"=>$this->_lang['errSysNNewsNameIsEmpty']),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong']),		
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			$type = $this->_input["t"];
			$typename = '';
			if ($type == 1) {
				$typename = '客户联系人';
			}elseif ($type == 2){
				$typename = '媒介联系人';
			}elseif ($type == 3){
				$typename = '供应商联系人';
			}
		
			$value_array = array();
			$value_array['id'] = $this->_input["id"];
			$value_array['company_id'] = $this->_input["hidCompanyId"];  //客户ID
			$value_array['realname'] = trim($this->_input["realname"]);  //名称
			$value_array['gender'] = $this->_input["gender"];  //性别
			$value_array['jobname'] = trim($this->_input["jobname"]);  //职位头衔
			$value_array['mobile'] = trim($this->_input["mobile"]);  //手机
			$value_array['office_call'] = trim($this->_input["office_call"]);  //办公电话
			$value_array['email'] = trim($this->_input["email"]);  //邮箱
			$value_array['im'] = trim($this->_input["im"]);  //MSN，QQ
			$value_array['address'] = trim($this->_input["address"]);  //地址
			$value_array['post'] = trim($this->_input["post"]);  //邮编
			$result = $this->obj_customer->updateCustomer($value_array);
			
			if ($result != true){
				$this->redirectPath('error','','更新'.$typename.'失败！',1);
			}else {
				//记录操作日志
				SystemPowerClass::addSysLog($typename.'->'.$typename.'管理->更新'.$typename);
				$this->redirectPath('error','./customer.manage.php?action=list&t='.$type,'更新'.$typename.'成功！',1);
			}
//		}
	}
	/**
	 * 信息查看
	 */
	function _view(){
		$id = $this->_input["id"];
		$type = $this->_input["t"];
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$id, "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$customer_array = $this->obj_customer->getCustomer($id, 'more');
			print_r($customer_array);
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('customer_array',$customer_array);
			$this->output('type',$type);
			$this->showpage('customer.view');
		}
	}
	
	/**
	 * 删除信息
	 */
	function _del(){
		$type = $this->_input["t"];
		$typename = '';
		if ($type == 1) {
			$typename = '客户联系人';
		}elseif ($type == 2){
			$typename = '媒介联系人';
		}elseif ($type == 3){
			$typename = '供应商联系人';
		}
		if (is_array($this->_input['id'])){
			foreach ($this->_input['id'] as $v) {
				$this->obj_customer->delCustomer($v);
			}
			//记录操作日志
			SystemPowerClass::addSysLog($typename.'->'.$typename.'管理->删除'.$typename);
			$this->redirectPath('error','','删除'.$typename.'成功！',1);
		}else {
			$this->redirectPath('error', '', '删除'.$typename.'失败！', 1);
		}
	}
	
	/**
	 * 返回页面的形式为 option格式
	 */
	function _ajax_get_customer(){
		
		if (!is_object($obj_ajax_page)) {
			require_once("ajaxcommonpage.class.php");
			$obj_ajax_page = new ajaxCommonPage();
		}
		$obj_ajax_page->pagebarnum(10);
		
		$cid = $this->_input['cid']; //公司ID
		$customer_name = Common::unescape($this->_input['txtCustomerName'],$this->_configinfo['websit']['ncharset']);
		$where = 'where company_id='.$cid;
		if ($customer_name != '') {
			$where = " and realname like '%".$customer_name."%' ";
		}
		$customer_array = $this->obj_customer->listCustomer($where, $obj_ajax_page);
		$page_list = $obj_ajax_page->show(2);

		/**
		 * 页面输出
		 */
		$this->output('page_list',$page_list);
		$this->output('customer_array',$customer_array);
		$this->showpage("sys_module_customer.result");
	}
}
$customer = new SysCustomerClass();
$customer->main();
unset($customer);
?>